﻿using System;
using System.Collections.Generic;
using System.Text;
using SAM.Framework.Repository.Entities;
using SAM.Framework.Repository.Connection;
using System.Linq;
using Dapper;
using DapperExtensions;

namespace SAM.Framework.RepositoryDapper
{
    public class DapperRepository<TEntity, TPrimaryKey> : AbstractDapperRepositoryBase<TEntity, TPrimaryKey>
        where TEntity : class, IEntity<TPrimaryKey>
    {
        public DapperRepository(BaseDbContext dbContext) : base(dbContext)
        {
        }

        public override void Delete(TEntity entity)
        {
            this.Connection.Delete(entity);
        }

        public override void Delete(TPrimaryKey id)
        {
            var entry = this.Get(id);
            this.Connection.Delete(entry);
        }

        public override IQueryable<TEntity> GetAll()
        { 
            return this.Connection.GetList<TEntity>().AsQueryable<TEntity>();
        }

        public override TEntity Insert(TEntity entity)
        {
            dynamic result = this.Connection.Insert<TEntity>(entity);
            return entity;
        }

        public override TEntity Update(TEntity entity)
        {
            this.Connection.Update(entity);
            return entity;
        }
    }
}
